
'use strict';

const cpt = require('crypto'),
    log4js = require('log4js'),
    util = require('util'),
    superagent = require('superagent');

const host = "http://itest.t.api.shaozi.com/"


function request(options, callback) {
    options.method = options.method || "get"
    let logger = log4js.getLogger(options.method);
    logger.debug(util.format('[%s] [token:%s]', options.url, options.token));
    let sa = superagent(options.method, host + options.url)
        .set('SZ-Version', ' 0.1.2')
        .set('Content-Type', 'application/json')
        .set('Accept', 'application/json')
        .set('Platform', "client")
    if (options.token) {
        sa = sa.set('Authorization', 'shaozi ' + options.token)
    }
    return sa
}

function md5(str) {
    var md5 = cpt.createHash("md5");
    md5.update(str);
    return md5.digest("hex").toUpperCase();
}

function upload(url, options) {
    let logger = log4js.getLogger('put');
    logger.debug(util.format('[%s] [%s]', url, JSON.stringify(options)));
    return superagent.put(url).send(options)
}

module.exports = {
    request: request,
    md5: md5,
    upload: upload
}

